﻿@model iChip.Hotspots.Models.FieldCreate

@{
    ViewBag.Title = "FieldCreate";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

<!-- BEGIN PAGE HEADER-->
<div class="row">
    <div class="col-md-12">
        <!-- BEGIN PAGE TITLE & BREADCRUMB-->
        <h3 class="page-title">
            Create Fields
        </h3>
        <ul class="page-breadcrumb breadcrumb">
            <li>
                <i class="icon-home"></i>
                <a href="/Admin">Tổng quan</a>
                <i class="icon-angle-right"></i>
            </li>
            <li><a href="#">FieldCreate</a></li>
            <li class="pull-right">
            </li>
        </ul>
        <!-- END PAGE TITLE & BREADCRUMB-->
    </div>
</div>
<!-- END PAGE HEADER-->
<div class="row">
    <div class="col-md-12">
        <div class="tabbable tabbable-custom boxless">
            <div class="tab-content">
                <div class="tab-pane active" id="tab_0">
                    <div class="portlet box red">
                        <div class="portlet-title">
                            <div class="caption"><i class="icon-reorder"></i>Tạo dữ liệu động</div>
                        </div>
                        <div class="portlet-body form">
                            <!-- BEGIN FORM-->
                            <div class="form-horizontal">
                                <div id="fieldcreate">
                                    @{Html.RenderAction("DefaultField","Admin");}
                                </div>
                                <div class="form-actions fluid">
                                    <div class="col-md-offset-3 col-md-9">
                                        <button type="button" class="btn blue" onclick="SaveField()">Lưu lại</button>
                                        <button type="button" class="btn green" onclick="AddField()">Thêm Field</button>
                                        <a type="button" class="btn default" href="@ViewBag.ReturnUrl">Hủy</a>
                                    </div>
                                </div>
                            </div>
                            <!-- END FORM-->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section Scripts {
    <script>
    var methodId = @ViewBag.MethodId;
    function AddField() {
        $.ajax({
            method: "GET",
            url: "/Admin/GenField",
        })
          .done(function (msg) {
              $("#fieldcreate").append(msg);
              App.init(); // initlayout and core plugins
          });
    }

    function DelField(id) {
        console.log("ID", id, $(id).closest("div.form-body"));
        $(id).closest("div.form-body").remove();
    }

    function SaveField() {
        var objRequest = [];
        var objForm = $("#fieldcreate div.form-body");
        if ($(objForm).length > 0) {
            for (var i = 0; i < $(objForm).length; i++) {
                var objItem = {};
                objItem.Name = $($(objForm)[i]).find("input[name=Field_Name]").val();
                objItem.Value = $($(objForm)[i]).find("select[name=Field_Type]").val();
                if($($(objForm)[i]).find("input[name=Field_IsMulti]:checked").length>0)
                {
                    objItem.IsMulti = true;
                }
                else{
                    objItem.IsMulti = false;
                }
                objItem.Method_Id = methodId;
                objRequest.push(objItem);
            }
        }
        $.ajax({
            method: "POST",
            url: "@Url.Action("FieldCreated","Admin")",
            data: JSON.stringify({ objRequest: objRequest }),
            contentType: "application/json",
        })
          .done(function (msg) {
              if(msg!=null)
              {
                  if(msg.Status==1){
                      window.location.href = '@ViewBag.ReturnUrl';
                  }
                  else{
                      alert(msg.Message);
                  }
              }
          });
    }
    </script>
}

